package ce.com.cenewbluesdk.uitl;

import java.util.Arrays;

public class ByteUtil {
    public static final byte[] EMPTY_BYTES = new byte[0];

    /**
     * byte[4]转换成int, little endian
     * @param b
     * @return
     */
    public static int byte4ToInt(byte[] b) {
        int v0 = (b[0] & 0x00ff);// &0xff将byte值无差异转成int,避免Java自动类型提升后,会保留高位的符号位
        int v1 = (b[1] & 0x00ff) << 8;
        int v2 = (b[2] & 0x00ff) << 16;
        int v3 = (b[3] & 0x00ff) << 24;
        return v0 + v1 + v2 + v3;
    }

    public static int byte3ToInt(byte[] b){
        int v0 = (b[0] & 0x00ff);// &0xff将byte值无差异转成int,避免Java自动类型提升后,会保留高位的符号位
        int v1 = (b[1] & 0x00ff) << 8;
        int v2 = (b[2] & 0x00ff) << 16;

        return v0 + v1 + v2;
    }

    /**
     * 把byte数组转换成int, little endian
     * @param bytes
     * @return
     */
    public static int byteToInt(byte... bytes) {
        if (bytes.length > 4) {
            throw new IllegalArgumentException("bytes的长度不能大于4: " + Arrays.toString(bytes));
        }
        int value = 0;
        for (int i = 0; i < bytes.length; i++) {
            value += (bytes[i] & 0xff) << 8 * i;
        }
        return value;
    }

    /**
     * int转换成byte[4], little endian
     * @param i
     * @return
     */
    public static byte[] intToByte4(int i) {
        byte[] b = new byte[4];
        b[0] = (byte) i;
        b[1] = (byte) (i >>> 8);
        b[2] = (byte) (i >>> 16);
        b[3] = (byte) (i >>> 24);
        return b;
    }

    public static byte[] intToByte(int value, int size) {
        if (size > 4) {
            throw new IllegalArgumentException(String.format("size(%s)不能大于4", size));
        }
        byte[] b = new byte[size];
        for (int i = 0; i < size; i++) {
            b[i] = (byte) (value >>> (8 * i));
        }
        return b;
    }

    public static byte[] subBytes(byte[] payload, int offset, int length) {
        byte[] bytes = new byte[length];
        System.arraycopy(payload, offset, bytes, 0, length);
        return bytes;
    }

    public static boolean[] getBooleanArray(byte b) {

        boolean[] array = new boolean[7];
        if (b == 0x7F) {
            array = new boolean[]{false, false, false, false, false, false, false};

        } else {
            for (int i = 0; i <= 6; i++) {
                array[i] = (1 == (b & 1));
                b = (byte) (b >> 1);
            }
        }
        return array;
    }

    /**
     * byte[2]转换成int, little endian
     * @param b
     * @return
     */
    public static int byte2ToInt(byte[] b) {
        int v0 = (int) (b[0] & 0x00ff);
        int v1 = ((int) (b[1] & 0x00ff)) << 8;
        return v0 + v1;
    }

    public static int byteToInt(byte[] payload, int offset, int length) {
        return byteToInt(subBytes(payload, offset, length));
    }

    /**
     * int转换成byte[4], big endian
     * big endian
     * @param i 整数
     * @return 代表整数的字节数组
     */
    public static byte[] intToByte4BE(int i) {
        byte[] b = new byte[4];
        b[0] = (byte) (i >>> 24);
        b[1] = (byte) (i >>> 16);
        b[2] = (byte) (i >>> 8);
        b[3] = (byte) i;
        return b;
    }

    /**
     * 将int转换成int[4], little endian
     * 对负数的处理存在问题, 废弃, 使用{@link #intToByte4(int)}替代
     * @param value
     * @return
     */
    @Deprecated
    public static byte[] intToByte(int value) {
        byte[] b = new byte[4];
        for (int i = 0; i < b.length; i++) {
            b[i] = (byte) ((value % 0x100) & 0xff);// 对0x100求余, 结果就在[0, 0xFF]区间内
            value = value / 0x100;
        }
        return b;
    }

    /*------------------------Legacy.START--------------------------*/
    @Deprecated
    public static String boolean2String(boolean bool) {
        return bool ? "1" : "0";
    }

    @Deprecated
    public static boolean string2Boolean(String number) {
        boolean bool = true;
        if ("0".equals(number)) {
            bool = false;
        }
        return bool;
    }

    @Deprecated
    public static short byteToShort3(byte[] b) {
        short v0 = (short) (b[0] & 0x00ff);
        short v1 = (short) (((b[1] & 0x00ff)) << 8);

        return (short) (v0 + v1);
    }

    @Deprecated
    public static byte[] shortToByteArray(short c) {
        byte[] b = new byte[2];
        b[0] = (byte) (c >>> 8);
        b[1] = (byte) c;
        return b;



    }

    public static byte[] int2bytes2(int c){
        byte[] bytes=new byte[2];
        bytes[0] = (byte) ((c) & 0xff);
        bytes[1] = (byte) ((c >>> 8) & 0xff);
        return bytes;
    }

    /**
     * String类型转换成ByteArray
     * @param str
     * @return
     */
    @Deprecated
    public static byte[] stringToByteArray(String str) {
        return str.getBytes();
    }

    /**
     * ByteArray类型的累加
     * @param byte1
     * @param byte2
     * @return
     */
    @Deprecated
    public static byte[] addByteArray(byte[] byte1, byte[] byte2) {
        byte[] data = new byte[byte1.length + byte2.length];
        System.arraycopy(byte1, 0, data, 0, byte1.length);
        System.arraycopy(byte2, 0, data, byte1.length, byte2.length);
        return data;
    }

    /**
     * 将int转换成int[4], little endian
     * @param b
     * @param value
     */
    @Deprecated
    public static void parseint2ByteArr(byte[] b, long value) {
        for (int i = 0; i < 4; i++) {
            b[i] = (byte) ((value % 0x100) & 0xff);// 对0x100求余, 结果就在[0, 0xFF]区间内
            value = value / 0x100;
        }
    }
    /*------------------------Legacy.END--------------------------*/

}
